<?php

require_once('functions/pageLoad.php');

$page_title = 'Business Directory Registration';

if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form'] == 'links' && strlen($_POST['first_name']) == 0){

$email_regex = "/^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/i";

$fail.= (strlen($_POST['company_name']) == 0) ? '<li>Your company name was not entered</li>' : '';
$fail.= (preg_match($email_regex, $_POST['email1']) == 0) ? '<li>Your email address is invalid</li>' : '';
$fail.= ($_POST['email1'] != $_POST['email2']) ? '<li>Email addresses do not match</li>' : '';
$fail.= (strlen($_POST['website']) == 0) ? '<li>Your website link was not entered</li>' : '';
$fail.= (strlen($_POST['linkback_address']) == 0) ? '<li>Your linkback address was not entered</li>' : '';
$fail.= (strlen($_POST['description']) == 0) ? '<li>Your business description was not entered</li>' : '';

if(strlen($_POST['website']) > 0 && strlen($_POST['linkback_address']) > 0){
$fail.= (strpos($_POST['linkback_address'], $_POST['website']) === false) ? '<li>Your linkback address must be a page from your specified website</li>' : '';
}

// check link back

if(strlen($_POST['linkback_address']) > 0){

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $_POST['linkback_address']);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$output = curl_exec($ch);
			curl_close($ch);
			$fail.= (strpos($output, 'href="http://www.iwantatradesman.co.uk') === false) ? '<li>We could not find a link to us on your website.</li>' : '';
			
}


if(strlen($fail) > 0){

// PHAIL

$results = '<p style="font-weight: bold; color: #cc0000;">Please correct the following errors;</p><ul style="font-weight: bold; color: #cc0000;">'.$fail.'</ul>';

}

else
{

	// insert address
	
	$url = clean_file_name($_POST['company_name']);
	
	if(strlen($_POST['line_1']) > 0){
	
		$address = $_POST['line_1'].",".$_POST['line_2'].",".$_POST['line_3'].",".$_POST['postcode'];
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/geo?q=".urlencode($address)."&output=csv&oe=utf8&sensor=false&key=".$settings['google_api_key']);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$output = curl_exec($ch);
			curl_close($ch);
		$outputArr = explode(",",$output);
		$latitude = $outputArr[2];
		$longitude = $outputArr[3];
		
	}
	
	$sql = "INSERT INTO directory SET 
			company_name = '".mysql_real_escape_string($_POST['company_name'])."',
			url = '".mysql_real_escape_string($url)."',
			area_id = '".mysql_real_escape_string($_POST['area_id'])."', 
			line_1 = '".mysql_real_escape_string($_POST['line_1'])."', 
			line_2 = '".mysql_real_escape_string($_POST['line_2'])."', 
			line_3 = '".mysql_real_escape_string($_POST['line_3'])."', 
			postcode = '".mysql_real_escape_string($_POST['postcode'])."',
			latitude = '".$latitude."',
			longitude = '".$longitude."',
			telephone = '".mysql_real_escape_string($_POST['telephone'])."',
			website = '".mysql_real_escape_string($_POST['website'])."',
			email = '".mysql_real_escape_string($_POST['email1'])."',
			linkback_address = '".mysql_real_escape_string($_POST['linkback_address'])."',
			description = '".mysql_real_escape_string($_POST['description'])."',
			linkback = 1
			";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$new_id = mysql_insert_id();
	
	$sql = "INSERT INTO directory_to_cats SET directory_id = '".$new_id."', category_id = '".mysql_real_escape_string($_POST['category_id'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

$results = '<p style="font-weight: bold; color: #009900;">Thank you! We have received your business directory listing request. We will notify you when your listing has been approved.</p>';

unset($_POST);

}

}

// load areas

	$sql = "SELECT id, area FROM areas ORDER BY area";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

	$area_dropdown = '<select name="city" id="city"><option value="">Please select</option>';

	while($rs=mysql_fetch_assoc($query)){
	$selected = ($_POST['city'] == $rs['id']) ? ' selected="selected"' : '' ;
	$area_dropdown.= '<option value="'.$rs['id'].'"'.$selected.'>'.htmlspecialchars($rs['area']).'</option>';
	}

	$area_dropdown.= '</select>';
	
// load categories

	$sql = "SELECT id, category FROM directory_cats ORDER BY category";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

	$category_dropdown = '<select name="category_id" id="category_id"><option value="">Please select</option>';

	while($rs=mysql_fetch_assoc($query)){
	$selected = ($_POST['category_id'] == $rs['id']) ? ' selected="selected"' : '' ;
	$category_dropdown.= '<option value="'.$rs['id'].'"'.$selected.'>'.htmlspecialchars($rs['category']).'</option>';
	}

	$category_dropdown.= '</select>';

include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<h1>Business Directory Registration</h1>

<?php echo $results; ?>

<div id="error_div" style="display: none;"></div>

<p>We offer business directory listings to websites that have content relevant to our users. Before completing the directory request form below, please ensure that you have a reciprocal link visible on your website that is easy for us to find. Please note that we periodically check for reciprocal links, if we cannot find a link to us on the page that you specify below we will remove your entry from our business directory.</p>

<p><strong>Example link</strong></p>
<p>&lt;a href="http://www.iwantatradesman.co.uk" title="I Want A Tradesman"&gt;I Want A Tradesman&lt;/a&gt;<br />&lt;p&gt;Online quotes from local tradesmen. Including; Plumbers, Electricians, Builders, Joiners and many more!&lt;/p&gt;</p>

<form method="post" id="links">

<input type="hidden" id="form" name="form" value="links" style="display: none;" />

<input type="text" id="first_name" name="first_name" value="" style="display: none;" />

<div id="registration_form">

<div class="rowWrapper">
<div class="rowLeft"><label for="company_name">Company name:</label></div>
<div class="rowRight"><input type="text" name="company_name" id="company_name" class="textbox" value="<?php echo $_POST['company_name']; ?>" /></div>
</div>

<div class="rowWrapper">
<div class="rowLeft"><label for="category_id">Business category:</label></div>
<div class="rowRight"><?php echo $category_dropdown; ?></div>
</div>

<div class="rowWrapper">
<div class="rowLeft"><label for="linkback_address">Linkback address:</label></div>
<div class="rowRight"><input type="text" name="linkback_address" id="linkback_address" class="textbox" value="<?php echo $_POST['linkback_address']; ?>" placeholder="The exact and absolute web address that links back to us." /></div>
</div>

<div class="rowWrapper">
<div class="rowLeft"><label for="website">Website:</label></div>
<div class="rowRight"><input type="text" name="website" id="website" class="textbox" value="<?php echo $_POST['website']; ?>" /></div>
</div>

<div class="rowWrapper" id="email1_wrap">
<div class="rowLeft"><label for="telephone">Telephone:</label></div>
<div class="rowRight"><input type="text" name="telephone" id="telephone" class="textbox" value="<?php echo $_POST['telephone']; ?>" placeholder="Only enter your telephone number if you would like it in your listing." /></div>
</div>

<div id="email_wrap">

<div class="rowWrapper" id="email1_wrap">
<div class="rowLeft"><label for="email1">Email address:</label></div>
<div class="rowRight"><input type="text" name="email1" id="email1" class="textbox" value="<?php echo $_POST['email1']; ?>" /></div>
</div>

<div class="rowWrapper" id="email2_wrap">
<div class="rowLeft"><label for="email2">Confirm email:</label></div>
<div class="rowRight"><input type="text" name="email2" id="email2" class="textbox" value="<?php echo $_POST['email2']; ?>" /></div>
</div>

</div>

<div id="address_wrap">

<div class="rowWrapper" id="line_1_wrap">
<div class="rowLeft"><label for="line_1">Address line 1:</label></div>
<div class="rowRight"><input type="text" name="line_1" id="line_1" class="textbox" value="<?php echo $_POST['line_1']; ?>" placeholder="Only enter your address sif you would like it in your listing." /></div>
</div>

<div class="rowWrapper" id="line_2_wrap">
<div class="rowLeft"><label for="line_2">Address line 2:</label></div>
<div class="rowRight"><input type="text" name="line_2" id="line_2" class="textbox" value="<?php echo $_POST['line_2']; ?>" /></div>
</div>

<div class="rowWrapper" id="line_3_wrap">
<div class="rowLeft"><label for="line_3">Address line 3:</label></div>
<div class="rowRight"><input type="text" name="line_3" id="line_3" class="textbox" value="<?php echo $_POST['line_3']; ?>" /></div>
</div>

<div class="rowWrapper" id="city_wrap">
<div class="rowLeft"><label for="city">Town/City:</label></div>
<div class="rowRight"><?php echo $area_dropdown; ?></div>
</div>

<div class="rowWrapper" id="postcode_wrap">
<div class="rowLeft"><label for="postcode">Postcode:</label></div>
<div class="rowRight"><input type="text" name="postcode" id="postcode" class="textbox" value="<?php echo $_POST['postcode']; ?>" style="width: 80px;" /></div>
</div>

</div>

<div class="rowWrapper" id="job_description_wrap">
<div class="rowLeft"><label for="description">Description:</label></div>
<div class="rowRight"><textarea name="description" id="description" class="textbox" rows="6" cols="30" placeholder="Describe your business and the services that you offer."><?php echo $_POST['description']; ?></textarea></div>
</div>


<div class="rowWrapper">
<div class="rowLeft">&nbsp;</div>
<div class="rowRight"><input type="image"  src="/images/register_button.gif" alt="Register" /></div>
</div>


</div>
</form>

<?php 

include('includes/rightColumn.php');
include('includes/footer.php');

?>